package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.school.domain.PacDanwei;
import com.ruoyi.school.domain.PacTeacher;
import com.ruoyi.school.domain.dto.ImportDanWeiInfo;
import com.ruoyi.school.mapper.PacDanweiMapper;
import com.ruoyi.school.service.PacDanweiService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author jeremylin
* @description 针对表【pac_danwei(单位用户表)】的数据库操作Service实现
* @createDate 2025-07-24 06:44:50
*/
@Service
public class PacDanweiServiceImpl extends ServiceImpl<PacDanweiMapper, PacDanwei>
    implements PacDanweiService {
    @Resource
    private ISysUserService userService;

    /**
     * 通过工号查找
     *
     * @param account
     * @return
     */
    @Override
    public PacDanwei getOneByDanWeiNum(String account) {
        return lambdaQuery().eq(PacDanwei::getDanWeiNum,account).one();
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<ImportDanWeiInfo> selectList() {
        List<PacDanwei> list = lambdaQuery().list();
        List<ImportDanWeiInfo> infoList = new ArrayList<>();
        List<Long> userIds = list.stream().map(PacDanwei::getUserId).map(Long::valueOf).collect(Collectors.toList());
        List<SysUser> sysUsers = userService.selectUserListByIds(userIds);
        Map<Long, SysUser> userMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));

        list.forEach(item -> {
            ImportDanWeiInfo info = new ImportDanWeiInfo();
            SysUser sysUser = userMap.get(item.getUserId().longValue());
            info.setName(item.getName());
            info.setBirth(item.getBirth());
            info.setSex(sysUser.getSex());
            info.setDuty(item.getDuty());
            info.setUnit(item.getUnit());
            info.setPhone(item.getPhone());
            info.setCity(item.getCity());
            infoList.add(info);
        });
        return infoList;
    }
}




